---
sidebar_position: 1
description: Learn how to publish and subscribe to messages using DeviceScript,
  Azure Event Hub, and Visual Studio Code extension.
keywords:
  - DeviceScript
  - Azure Event Hub
  - Visual Studio Code
  - publish message
  - subscribe message
---
# Messages

## Publish Message

Publish a topic, payload pair to the gateway.

DeviceScript will stringify and upload your object as a JSON payload. The gateway add this messages
into an [Azure Event Hub](https://azure.microsoft.com/en-us/products/event-hubs/), additonal sinks can be configured online.

```ts
import { publishMessage } from "@devicescript/cloud"

// highlight-next-line
await publishMessage("greetings", { msg: "hello" })
```

In the device gateway, this topic will be expanded to `devs/{deviceid}/from/{topic}`.
If you want to override this behavior, use `/{topic}` and the leading will be removed
the MQTT topic to be `{topic}`.

:::tip

Keep those field names very short as the payload needs to be small (`< 236` bytes).

:::

In the Visual Studio Code extension, you can upload a JSON
message from the device context menu.

## Subscribe Message

Subscribes to able to receive messages from the gateway.

```ts
import { subscribeMessage } from "@devicescript/cloud"

subscribeMessage("greetings", async msg => {
    console.log(msg)
})
```

## Visual Studio Code extension

In the DeviceScript Gateway extension, you can connect to the device and
see the messages in the output window.
